#
#   api_person.py
#
#   David Janes
#   2009.01.30
#
#	Copyright 2009 David Janes
#

import os
import os
import sys
import urllib
import types
import pprint
import types
import time
import datetime

try:
	import json
except:
	import simplejson as json

import bm_extract
import bm_uri
import api
import bm_work

from bm_log import Log

import mfdict
import vcard
import uf_address
import hcard

class Person(api.APIBase):
	base = None

	def GetMeta(self):
		hd = mfdict.mfdict()

		#
		#	Free text
		#
		for key, value in self._request_meta.iteritems():
			if key not in [ "free.address", "free" ]:
				continue

			parser = uf_address.TextAddressParser(hint_has_name = (key == "free"))
			parser.FeedOne(value)

			for card in parser.Iterate():
				for card_key, card_value in card.iteritems():
					hd[card_key] = card_value

				break

		#
		#	Explicitly structured
		#
		for key, value in self._request_meta.iteritems():
			if key in [ "free.address", "free", ]:
				continue

			hd[key] = value

		#
		#	Clean up & return
		#
		vcard.scrub(hd, base = self.base)
			
		return	vcard.decompose(hd)

if __name__ == '__main__':
	api = Person()
	api.request = {
		"fn" : "Janes, David",
		"street-name" : "182 Old Orchard Grove",
		"locality" : "Toronto",
		"postal-code" : "M5M 2D9",
		"photo" : "../../images/Thumbnail.aspx?Item=1d9aa41e-71b1-4008-a120-278182788df5",
		"tel.voice" : "tel:647-866-6149",
		"base" : "http://www2.parl.gc.ca/parlinfo/compilations/HouseOfCommons/MemberByPostalCode.aspx?Submit=Find&Language=E&PostalCode=A1B2B3",
	};
	pprint.pprint(api.response, width = 1)

	apio = Person()
	apio.request = {
		"fn" : "Janes, David",
		"free.address" : u"""
542 Talbot St 
St. Thomas, ON N5P 1C4
Tel: 519-631-0666
Toll Free: 1-800-265-7638
Fax: 519-631-9478
speters.mpp.co@liberal.ola.org
		""",
	};
	pprint.pprint(apio.response, width = 1)

